package grape.house.rest.buildingunit.mvc;

import com.baomidou.mybatisplus.core.metadata.IPage;
import grape.common.exception.runtime.RBaseException;
import grape.house.rest.buildingunit.form.BuildingUnitListForm;
import grape.house.service.buildingunit.query.BuildingUnitListQuery;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import javax.validation.Valid;
import grape.house.rest.buildingunit.form.BuildingUnitCreateForm;
import grape.house.rest.buildingunit.form.BuildingUnitUpdateForm;
import grape.house.rest.buildingunit.form.BuildingUnitListPageForm;
import grape.house.rest.buildingunit.vo.BuildingUnitVo;
import grape.house.rest.buildingunit.mapper.BuildingUnitWebMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import grape.common.rest.mvc.BaseController;
import grape.house.service.buildingunit.po.BuildingUnit;
import grape.house.service.buildingunit.api.IBuildingUnitService;
import java.util.List;
/**
 * <p>
 * 楼栋单元 前端控制器
 * </p>
 *
 * @author yangwei
 * @since 2020-09-17
 */
@RestController
@RequestMapping("/house/buildingunit")
@Api(tags = "楼栋单元相关接口")
public class BuildingUnitController extends BaseController<BuildingUnitVo, BuildingUnit> {

    @Autowired
    private BuildingUnitWebMapper currentWebMapper;
    @Autowired
    private IBuildingUnitService currentService;



     @ApiOperation("添加楼栋单元")
     @PreAuthorize("hasAuthority('buildingunit:single:create')")
     @PostMapping
     @ResponseStatus(HttpStatus.CREATED)
     public BuildingUnitVo create(@RequestBody @Valid BuildingUnitCreateForm cf) {

         // code 唯一检查
         if (currentService.existCode(cf.getCode())) {
             throw new RBaseException("编码已存在");
         }
         BuildingUnit po = currentWebMapper.formToPo(cf);
         return super.create(po);
     }

     @ApiOperation("根据ID查询楼栋单元")
     @PreAuthorize("hasAuthority('buildingunit:single:queryById')")
     @GetMapping("/{id}")
     @ResponseStatus(HttpStatus.OK)
     public BuildingUnitVo queryById(@PathVariable String id) {
         return super.queryById(id);
     }

     @ApiOperation("删除楼栋单元")
     @PreAuthorize("hasAuthority('buildingunit:single:deleteById')")
     @DeleteMapping("/{id}")
     @ResponseStatus(HttpStatus.NO_CONTENT)
     public boolean deleteById(@PathVariable String id) {
         return super.deleteById(id);
     }

     @ApiOperation("更新楼栋单元")
     @PreAuthorize("hasAuthority('buildingunit:single:updateById')")
     @PutMapping("/{id}")
     @ResponseStatus(HttpStatus.CREATED)
     public BuildingUnitVo update(@PathVariable String id,@RequestBody @Valid BuildingUnitUpdateForm uf) {
         BuildingUnit po = currentWebMapper.formToPo(uf);
         po.setId(id);
         return super.update(po);
     }

    @ApiOperation("不分页查询楼栋单元")
    @PreAuthorize("hasAuthority('buildingunit:single:list')")
    @GetMapping("/list")
    @ResponseStatus(HttpStatus.OK)
    public List<BuildingUnitVo> list(BuildingUnitListForm listForm) {
        BuildingUnit po = currentWebMapper.formToPo(listForm);
        BuildingUnitListQuery buildingUnitListQuery = new BuildingUnitListQuery();
        buildingUnitListQuery.setPoQuery(po);
        return super.list(buildingUnitListQuery);
    }

    @ApiOperation("分页查询楼栋单元")
    @PreAuthorize("hasAuthority('buildingunit:single:listPage')")
    @GetMapping("/listPage")
    @ResponseStatus(HttpStatus.OK)
    public IPage<BuildingUnitVo> listPage(BuildingUnitListPageForm listPageForm) {
         BuildingUnit po = currentWebMapper.formToPo(listPageForm);
        BuildingUnitListQuery buildingUnitListQuery = new BuildingUnitListQuery();
        buildingUnitListQuery.setPoQuery(po);
         return super.listPage(buildingUnitListQuery,listPageForm);
     }

}
